package com.example.tableapplication;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.tableapplication.application.MyApplication;

import cn.bertsir.zbar.Qr.ScanResult;
import cn.bertsir.zbar.QrConfig;
import cn.bertsir.zbar.QrManager;
import cn.bertsir.zbar.utils.QRUtils;
import cn.bertsir.zbar.view.ScanLineView;


public class MainActivity_1 extends AppCompatActivity implements View.OnClickListener {

    private Button bt_scan;
    private Button bt_make;
    private Button bt_save;
    private static final String TAG = "MainActivity_1";
    private ImageView iv_qr;
    private EditText et_qr_content;
    private EditText et_qr_title;
    private EditText et_qr_des;
    private CheckBox cb_show_title;
    private CheckBox cd_show_des;
    private CheckBox cb_show_ding;
    private CheckBox cb_show_custom_ding;
    private CheckBox cb_show_flash;
    private CheckBox cb_show_album;
    private CheckBox cb_only_center;
    private CheckBox cb_create_add_water;
    private CheckBox cb_crop_image;
    private CheckBox cb_show_zoom;
    private CheckBox cb_auto_zoom;
    private CheckBox cb_finger_zoom;
    private CheckBox cb_double_engine;
    private CheckBox cb_loop_scan;
    private CheckBox cb_auto_light;
    private CheckBox cb_have_vibrator;
    private RadioButton rb_qrcode;
    private RadioButton rb_bcode;
    private RadioButton rb_all;
    private RadioButton rb_screen_sx;
    private RadioButton rb_screen_hx;
    private RadioButton rb_screen_auto;
    private EditText et_loop_scan_time;
    private RadioButton rb_scanline_radar;
    private RadioButton rb_scanline_grid;
    private RadioButton rb_scanline_hybrid;
    private RadioButton rb_scanline_line;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_1);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        MyApplication myApplicationScan = (MyApplication) getApplication();
//        initView2();
//        initView1();
        //判断是否修改并保存过扫描设置，若修改并保存过则调initView1()来获取保存到全局变量的各控件的状态（只恢复了复选框和单选控件的状态）
        // 否则只用initView1()初始化界面默认初始化状态
        if(myApplicationScan.getMyState()){
            initView2();
            initView1();
        }else{
            initView2();
        }
    }

    //设置各个复选框和单选控件的状态为全局变量MyApplication类中保存的状态
    public void initView1(){
        MyApplication myApplicationScan = (MyApplication) getApplication();
//        iv_qr = myApplicationScan.getIv_qr();
        et_qr_content.setText(myApplicationScan.getEt_qr_content());
        et_qr_title.setText(myApplicationScan.getEt_qr_title());
        et_qr_des.setText(myApplicationScan.getEt_qr_des());

//        cb_show_title = myApplication.getCb_show_title();
//        cd_show_des = myApplication.getCd_show_des();
//        cb_show_ding = myApplication.getCb_show_ding();
//        cb_show_custom_ding = myApplication.getCb_show_custom_ding();
//        cb_show_flash = myApplication.getCb_show_flash();
//        cb_show_album = myApplication.getCb_show_album();
//        cb_only_center = myApplication.getCb_only_center();
//        cb_create_add_water = myApplication.getCb_create_add_water();
//        cb_crop_image = myApplication.getCb_crop_image();
//        cb_show_zoom = myApplication.getCb_show_zoom();
//        cb_auto_zoom = myApplication.getCb_auto_zoom();
//        cb_finger_zoom = myApplication.getCb_finger_zoom();
//        cb_double_engine = myApplication.getCb_double_engine();
//        cb_loop_scan = myApplication.getCb_loop_scan();
//        cb_auto_light = myApplication.getCb_auto_light();
//        cb_have_vibrator = myApplication.getCb_have_vibrator();
//        rb_qrcode = myApplication.getRb_qrcode();
//        rb_bcode = myApplication.getRb_bcode();
//        rb_all = myApplication.getRb_all();
//        rb_screen_sx = myApplication.getRb_screen_sx();
//        rb_screen_hx = myApplication.getRb_screen_hx();
//        rb_screen_auto = myApplication.getRb_screen_auto();
//        et_loop_scan_time = myApplication.getEt_loop_scan_time();
//        rb_scanline_radar = myApplication.getRb_scanline_radar();
//        rb_scanline_grid = myApplication.getRb_scanline_grid();
//        rb_scanline_hybrid = myApplication.getRb_scanline_hybrid();
//        rb_scanline_line = myApplication.getRb_scanline_line();

        cb_show_title.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_show_title()));
        cd_show_des.setChecked(Boolean.parseBoolean(myApplicationScan.getCd_show_des()));
        cb_show_ding.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_show_ding()));
        cb_show_custom_ding.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_show_custom_ding()));
        cb_show_flash.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_show_flash()));
        cb_show_album.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_show_album()));
        cb_only_center.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_only_center()));
        cb_create_add_water.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_create_add_water()));
        cb_crop_image.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_crop_image()));
        cb_show_zoom.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_show_zoom()));
        cb_auto_zoom.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_auto_zoom()));
        cb_finger_zoom.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_finger_zoom()));
        cb_double_engine.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_double_engine()));
        cb_loop_scan.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_loop_scan()));
        cb_auto_light.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_auto_light()));
        cb_have_vibrator.setChecked(Boolean.parseBoolean(myApplicationScan.getCb_have_vibrator()));
        rb_qrcode.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_qrcode()));
        rb_bcode.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_bcode()));
        rb_all.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_all()));
        rb_screen_sx.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_screen_sx()));
        rb_screen_hx.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_screen_hx()));
        rb_screen_auto.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_screen_auto()));
        et_loop_scan_time.setText(myApplicationScan.getEt_loop_scan_time());
        rb_scanline_radar.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_scanline_radar()));
        rb_scanline_grid.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_scanline_grid()));
        rb_scanline_hybrid.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_scanline_hybrid()));
        rb_scanline_line.setChecked(Boolean.parseBoolean(myApplicationScan.getRb_scanline_line()));
    }
    //与xml文件中的各个控件绑定，状态为初始化状态
    private void initView2() {
        bt_scan = (Button) findViewById(R.id.bt_scan);
        bt_scan.setOnClickListener(this);
        iv_qr = (ImageView) findViewById(R.id.iv_qr);
        iv_qr.setOnClickListener(this);
        bt_make = (Button) findViewById(R.id.bt_make);
        bt_make.setOnClickListener(this);
        bt_save = (Button) findViewById(R.id.bt_save);
        bt_save.setOnClickListener(this);


        iv_qr.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String s = null;
                try {
                    s = QRUtils.getInstance().decodeQRcode(iv_qr);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAG, "onLongClickException: " + e.toString());
                }
                Toast.makeText(getApplicationContext(), "内容：" + s, Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        et_qr_content = (EditText) findViewById(R.id.et_qr_content);
        et_qr_title = (EditText) findViewById(R.id.et_qr_title);
        et_qr_des = (EditText) findViewById(R.id.et_qr_des);

        cb_show_title = (CheckBox) findViewById(R.id.cb_show_title);
        cd_show_des = (CheckBox) findViewById(R.id.cd_show_des);
        cb_show_ding = (CheckBox) findViewById(R.id.cb_show_ding);
        cb_show_custom_ding = (CheckBox) findViewById(R.id.cb_show_custom_ding);
        cb_show_flash = (CheckBox) findViewById(R.id.cb_show_flash);
        cb_show_album = (CheckBox) findViewById(R.id.cb_show_album);
        cb_only_center = (CheckBox) findViewById(R.id.cb_only_center);
        cb_crop_image = (CheckBox) findViewById(R.id.cb_crop_image);
        cb_create_add_water = (CheckBox) findViewById(R.id.cb_create_add_water);
        cb_show_zoom = (CheckBox) findViewById(R.id.cb_show_zoom);
        cb_auto_zoom = (CheckBox) findViewById(R.id.cb_auto_zoom);
        cb_finger_zoom = (CheckBox) findViewById(R.id.cb_finger_zoom);
        cb_double_engine = (CheckBox) findViewById(R.id.cb_double_engine);
        cb_loop_scan = (CheckBox) findViewById(R.id.cb_loop_scan);
        rb_qrcode = (RadioButton) findViewById(R.id.rb_qrcode);
        rb_bcode = (RadioButton) findViewById(R.id.rb_bcode);
        rb_all = (RadioButton) findViewById(R.id.rb_all);
        rb_screen_hx = (RadioButton) findViewById(R.id.rb_screen_hx);
        rb_screen_sx = (RadioButton) findViewById(R.id.rb_screen_sx);
        rb_screen_auto = (RadioButton) findViewById(R.id.rb_screen_auto);
        et_loop_scan_time = (EditText) findViewById(R.id.et_loop_scan_time);

        rb_qrcode.setChecked(true);
        rb_screen_sx.setChecked(true);

        rb_scanline_radar = (RadioButton) findViewById(R.id.rb_scanline_radar);
        rb_scanline_radar.setOnClickListener(this);
        rb_scanline_grid = (RadioButton) findViewById(R.id.rb_scanline_grid);
        rb_scanline_grid.setOnClickListener(this);
        rb_scanline_hybrid = (RadioButton) findViewById(R.id.rb_scanline_hybrid);
        rb_scanline_hybrid.setOnClickListener(this);
        rb_scanline_line = (RadioButton) findViewById(R.id.rb_scanline_line);
        rb_scanline_line.setOnClickListener(this);
        cb_auto_light = (CheckBox) findViewById(R.id.cb_auto_light);
        cb_have_vibrator = (CheckBox) findViewById(R.id.cb_have_vibrator);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_scan:
                start();
                break;
            case R.id.bt_make:
                Bitmap qrCode = null;
                if (cb_create_add_water.isChecked()) {
                    qrCode = QRUtils.getInstance().createQRCodeAddLogo(et_qr_content.getText().toString(),
                            BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round));
                } else {
                    qrCode = QRUtils.getInstance().createQRCode(et_qr_content.getText().toString());

                }
                iv_qr.setImageBitmap(qrCode);
                Toast.makeText(getApplicationContext(), "长按可识别", Toast.LENGTH_LONG).show();
                break;
            case R.id.bt_save:
                save();
                break;
        }
    }

    //扫描按钮点击事件调用的方法
    private void start() {
        int scan_type = 0;
        int scan_view_type = 0;
        int screen = 1;
        int line_style = ScanLineView.style_radar;
        if (rb_all.isChecked()) {
            scan_type = QrConfig.TYPE_ALL;
            scan_view_type = QrConfig.SCANVIEW_TYPE_QRCODE;
        } else if (rb_qrcode.isChecked()) {
            scan_type = QrConfig.TYPE_QRCODE;
            scan_view_type = QrConfig.SCANVIEW_TYPE_QRCODE;
        } else if (rb_bcode.isChecked()) {
            scan_type = QrConfig.TYPE_BARCODE;
            scan_view_type = QrConfig.SCANVIEW_TYPE_BARCODE;
        }
        if (rb_screen_auto.isChecked()) {
            screen = QrConfig.SCREEN_SENSOR;
        } else if (rb_screen_sx.isChecked()) {
            screen = QrConfig.SCREEN_PORTRAIT;
        } else if (rb_screen_hx.isChecked()) {
            screen = QrConfig.SCREEN_LANDSCAPE;
        }

        if (rb_scanline_radar.isChecked()) {
            line_style = ScanLineView.style_radar;
        } else if (rb_scanline_grid.isChecked()) {
            line_style = ScanLineView.style_gridding;
        } else if (rb_scanline_hybrid.isChecked()) {
            line_style = ScanLineView.style_hybrid;
        } else if (rb_scanline_line.isChecked()) {
            line_style = ScanLineView.style_line;
        }


        QrConfig qrConfig = new QrConfig.Builder()
                .setDesText(et_qr_des.getText().toString())//扫描框下文字
                .setShowDes(cd_show_des.isChecked())//是否显示扫描框下面文字
                .setShowLight(cb_show_flash.isChecked())//显示手电筒按钮
                .setShowTitle(cb_show_title.isChecked())//显示Title
                .setShowAlbum(cb_show_album.isChecked())//显示从相册选择按钮
                .setNeedCrop(cb_crop_image.isChecked())//是否从相册选择后裁剪图片
                .setCornerColor(Color.parseColor("#E42E30"))//设置扫描框颜色
                .setLineColor(Color.parseColor("#E42E30"))//设置扫描线颜色
                .setLineSpeed(QrConfig.LINE_MEDIUM)//设置扫描线速度
                .setScanType(scan_type)//设置扫码类型（二维码，条形码，全部，自定义，默认为二维码）
                .setScanViewType(scan_view_type)//设置扫描框类型（二维码还是条形码，默认为二维码）
                .setCustombarcodeformat(QrConfig.BARCODE_PDF417)//此项只有在扫码类型为TYPE_CUSTOM时才有效
                .setPlaySound(cb_show_ding.isChecked())//是否扫描成功后bi~的声音
                .setDingPath(cb_show_custom_ding.isChecked() ? R.raw.test : R.raw.qrcode)//设置提示音(不设置为默认的Ding~)
                .setIsOnlyCenter(cb_only_center.isChecked())//是否只识别框中内容(默认为全屏识别)
                .setTitleText(et_qr_title.getText().toString())//设置Tilte文字
                .setTitleBackgroudColor(Color.parseColor("#262020"))//设置状态栏颜色
                .setTitleTextColor(Color.WHITE)//设置Title文字颜色
                .setShowZoom(cb_show_zoom.isChecked())//是否开始滑块的缩放
                .setAutoZoom(cb_auto_zoom.isChecked())//是否开启自动缩放(实验性功能，不建议使用)
                .setFingerZoom(cb_finger_zoom.isChecked())//是否开始双指缩放
                .setDoubleEngine(cb_double_engine.isChecked())//是否开启双引擎识别(仅对识别二维码有效，并且开启后只识别框内功能将失效)
                .setScreenOrientation(screen)//设置屏幕方式
                .setOpenAlbumText("选择要识别的图片")//打开相册的文字
                .setLooperScan(cb_loop_scan.isChecked())//是否连续扫描二维码
                .setLooperWaitTime(Integer.parseInt(et_loop_scan_time.getText().toString()) * 1000)//连续扫描间隔时间
                .setScanLineStyle(line_style)//扫描线样式
                .setAutoLight(cb_auto_light.isChecked())//自动灯光
                .setShowVibrator(cb_have_vibrator.isChecked())//是否震动提醒
                .create();
        QrManager.getInstance().init(qrConfig).startScan(MainActivity_1.this, new QrManager.OnScanResultCallback() {
            @Override
            public void onScanSuccess(ScanResult result) {
                Log.e(TAG, "onScanSuccess: " + result);
                Toast.makeText(getApplicationContext(), "内容：" + result.getContent()
                        + "  类型：" + result.getType(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    //保存按钮的点击事件调用方法，保存各个控件的状态
    private void save(){
        MyApplication myApplicationScan = (MyApplication) getApplication();
        myApplicationScan.setMyState(true);
//        myApplicationScan.setIv_qr(iv_qr);
        myApplicationScan.setEt_qr_content(et_qr_content.getText().toString());
        myApplicationScan.setEt_qr_title(et_qr_title.getText().toString());
        myApplicationScan.setEt_qr_des(et_qr_des.getText().toString());
        myApplicationScan.setCb_show_title(String.valueOf(cb_show_title.isChecked()));
        myApplicationScan.setCd_show_des(String.valueOf(cd_show_des.isChecked()));
        myApplicationScan.setCb_show_ding(String.valueOf(cb_show_ding.isChecked()));
        myApplicationScan.setCb_show_custom_ding(String.valueOf(cb_show_custom_ding.isChecked()));
        myApplicationScan.setCb_show_flash(String.valueOf(cb_show_flash.isChecked()));
        myApplicationScan.setCb_show_album(String.valueOf(cb_show_album.isChecked()));
        myApplicationScan.setCb_only_center(String.valueOf(cb_only_center.isChecked()));
        myApplicationScan.setCb_create_add_water(String.valueOf(cb_create_add_water.isChecked()));
        myApplicationScan.setCb_crop_image(String.valueOf(cb_crop_image.isChecked()));
        myApplicationScan.setCb_show_zoom(String.valueOf(cb_show_zoom.isChecked()));
        myApplicationScan.setCb_auto_zoom(String.valueOf(cb_auto_zoom.isChecked()));
        myApplicationScan.setCb_finger_zoom(String.valueOf(cb_finger_zoom.isChecked()));
        myApplicationScan.setCb_double_engine(String.valueOf(cb_double_engine.isChecked()));
        myApplicationScan.setCb_loop_scan(String.valueOf(cb_loop_scan.isChecked()));
        myApplicationScan.setCb_auto_light(String.valueOf(cb_auto_light.isChecked()));
        myApplicationScan.setCb_have_vibrator(String.valueOf(cb_have_vibrator.isChecked()));
        myApplicationScan.setRb_qrcode(String.valueOf(rb_qrcode.isChecked()));
        myApplicationScan.setRb_bcode(String.valueOf(rb_bcode.isChecked()));
        myApplicationScan.setRb_all(String.valueOf(rb_all.isChecked()));
        myApplicationScan.setRb_screen_sx(String.valueOf(rb_screen_sx.isChecked()));
        myApplicationScan.setRb_screen_hx(String.valueOf(rb_screen_hx.isChecked()));
        myApplicationScan.setRb_screen_auto(String.valueOf(rb_screen_auto.isChecked()));
        myApplicationScan.setEt_loop_scan_time(et_loop_scan_time.getText().toString());
        myApplicationScan.setRb_scanline_radar(String.valueOf(rb_scanline_radar.isChecked()));
        myApplicationScan.setRb_scanline_grid(String.valueOf(rb_scanline_grid.isChecked()));
        myApplicationScan.setRb_scanline_hybrid(String.valueOf(rb_scanline_hybrid.isChecked()));
        myApplicationScan.setRb_scanline_line(String.valueOf(rb_scanline_line.isChecked()));
        myApplicationScan.setMyState(true);
        myApplicationScan.saveManger();
        Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        System.out.println("保存成功");

    }



//    private void initEvent() {
//        findViewById(R.id.bt_save).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                showDialog();
//            }
//        });
//    }

    public static void showDialog(Context context,String result){
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
//        builder.setIcon(R.drawable.asdf);
//        List list=new DbUtils().QureySql();
        builder.setTitle("提示");
        builder.setMessage(result);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
        AlertDialog dialog=builder.create();
        dialog.show();

    }



}
